package com.neusoft.SPNursingHome.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.SPNursingHome.po.CareRecord;
import com.neusoft.SPNursingHome.po.CareRecordWithAdminAndCareItem;
import com.neusoft.SPNursingHome.po.CareRecordWithAdminAndCareItem;
import com.neusoft.SPNursingHome.po.CareRecordWithAdminAndCareItemAndClient;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface CareRecordDao extends BaseMapper<CareRecord> {

    @Select({
            "SELECT cr.*, a.ad_name, ci.care_item_name", // 添加 care_item_name
            "FROM care_record cr",
            "LEFT JOIN admin a ON cr.ad_id = a.ad_id",
            "LEFT JOIN care_item ci ON cr.care_item_id = ci.care_item_id", // 联接 care_item 表
            "WHERE cr.client_id = #{clientId} ",
            "AND cr.care_record_status = 1",
            "AND ci.care_item_name LIKE CONCAT('%', #{careItemName}, '%')" // 添加护理项目名称筛选条件
    })
    List<CareRecordWithAdminAndCareItem> selectByClientIdAndCareItemName(
            @Param("clientId") Integer clientId,
            @Param("careItemName") String careItemName
    );
    @Select("<script>"
            + "SELECT cr.*, a.ad_name, ci.care_item_name, c.client_name "
            + "FROM care_record cr "
            + "LEFT JOIN admin a ON cr.ad_id = a.ad_id "
            + "LEFT JOIN care_item ci ON cr.care_item_id = ci.care_item_id "
            + "LEFT JOIN client c ON cr.client_id = c.client_id "
            + "WHERE cr.ad_id = #{adminId} "
            + "  AND cr.care_record_status = 1 "
            + "<if test='careItemName != null and careItemName.trim().length() > 0'>"
            + "  AND ci.care_item_name LIKE CONCAT('%', #{careItemName}, '%') "
            + "</if>"
            + "<if test='careContent != null and careContent.trim().length() > 0'>"
            + "  AND cr.care_content LIKE CONCAT('%', #{careContent}, '%') "
            + "</if>"
            + "ORDER BY cr.care_time DESC"
            + "</script>")
    IPage<CareRecordWithAdminAndCareItemAndClient> selectPageByAdminAndFilters(
            Page<?> page,
            @Param("adminId") Integer adminId,
            @Param("careItemName") String careItemName,
            @Param("careContent") String careContent
    );
    @Select({
            "SELECT cr.*, a.ad_name, ci.care_item_name", // 添加 care_item_name
            "FROM care_record cr",
            "LEFT JOIN admin a ON cr.ad_id = a.ad_id",
            "LEFT JOIN care_item ci ON cr.care_item_id = ci.care_item_id", // 联接 care_item 表
            "WHERE cr.client_id = #{clientId} ",
            "AND cr.care_record_status = 1"
    })
    List<CareRecordWithAdminAndCareItem> selectByClientId(
            @Param("clientId") Integer clientId);


    // 逻辑删除客户护理记录
    // 更新护理记录状态为0
    @Update("UPDATE care_record SET care_record_status = 0 WHERE care_record_id = #{careRecordId}")
    int updateStatusToInactive(@Param("careRecordId") Integer careRecordId);

    // 查询所有状态为1的护理记录
    @Select("SELECT * FROM care_record WHERE care_record_status = 1")
    List<CareRecord> selectActiveRecords();

    @Select({
            "SELECT cr.*, a.ad_name, ci.care_item_name, c.client_name",
            "FROM care_record cr",
            "LEFT JOIN admin a ON cr.ad_id = a.ad_id",
            "LEFT JOIN care_item ci ON cr.care_item_id = ci.care_item_id",
            "LEFT JOIN client c ON cr.client_id = c.client_id",
            "WHERE cr.care_record_id = #{careRecordId}"
    })
    CareRecordWithAdminAndCareItemAndClient selectByRecordId(
            @Param("careRecordId") Integer careRecordId
    );
}
